//
//  LaunchPagesVC.M
//  EPTool
//
//  Created by guoshiqing on 2018/4/11.
//  Copyright © 2018年 云数动能科技. All rights reserved.
//

#import "LaunchPagesVC.h"
#import "Macro.h"
#import "UIView+DDAddition.h"


#define btn_w (UI_SCREEN_WIDTH-2*24.f-17.f)/2
#define regBtn_x (UI_SCREEN_WIDTH-btn_w-24.f)
#define btn_y (UI_SCREEN_HEIGHT-IPhoneX_FixHeight-Cell_H-17.f)
#define VERSION_INFO_CURRENT    @"currentversion"
#define VERSION_IS_FIRSTGUIDE   @"isShowFirstGuide"


@interface LaunchPagesVC ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) UIButton *btnEnter;
@property (nonatomic, strong) UIButton *btnLogin;
@property (nonatomic, strong) UIButton *btnRegister;
@end

@implementation LaunchPagesVC

- (void)launchPageControllerWithImages:(NSArray *)images
{
    UIScrollView *gui = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, UI_SCREEN_HEIGHT)];
    gui.delegate = self;
    gui.pagingEnabled = YES;
    // 隐藏滑动条
    gui.showsHorizontalScrollIndicator = NO;
    gui.showsVerticalScrollIndicator = NO;
    // 取消反弹
    gui.bounces = NO;
    for (NSInteger i = 0; i < images.count; i ++) {
        [gui addSubview:({
            self.btnEnter = [UIButton buttonWithType:UIButtonTypeCustom];
            self.btnEnter.frame = CGRectMake(UI_SCREEN_WIDTH * i, 0, UI_SCREEN_WIDTH, UI_SCREEN_HEIGHT);
            self.btnEnter.userInteractionEnabled = NO;
            [self.btnEnter setImage:[UIImage imageNamed:images[i]] forState:UIControlStateNormal];
            self.btnEnter.imageView.contentMode = UIViewContentModeScaleAspectFit;
            self.btnEnter;
        })];
    }
    gui.contentSize = CGSizeMake(UI_SCREEN_WIDTH * images.count, 0);
    [self.view addSubview:gui];
    
    // pageControl
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH / 2, 30.f)];
    self.pageControl.centerX = UI_SCREEN_WIDTH / 2;
    self.pageControl.bottom = btn_y - 20.f;
    [self.view addSubview:self.pageControl];
    self.pageControl.pageIndicatorTintColor = [EPColor epColor_UnderLine];
    self.pageControl.currentPageIndicatorTintColor = RGB_BLUE;
    self.pageControl.userInteractionEnabled = NO;
    self.pageControl.numberOfPages = images.count;
    
    [self.view addSubview:self.btnLogin];
    [self.view addSubview:self.btnRegister];
}

- (UIButton *)btnLogin
{
    if (!_btnLogin) {
        _btnLogin = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnLogin setTitle:@"登录" forState:UIControlStateNormal];
        _btnLogin.frame = CGRectMake(24.f, btn_y, btn_w, Cell_H);
        _btnLogin.layer.cornerRadius = CornerRadius;
        _btnLogin.layer.borderWidth = 1.f;
        _btnLogin.layer.borderColor = [EPColor epColor_UnderLine].CGColor;
        _btnLogin.clipsToBounds = YES;
        [_btnLogin setBackgroundColor:[UIColor whiteColor]];
        [_btnLogin setTitleColor:RGB_BLUE forState:UIControlStateNormal];
        [_btnLogin addTarget:self action:@selector(btnLoginClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnLogin;
}

- (UIButton *)btnRegister
{
    if (!_btnRegister) {
        _btnRegister = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnRegister setTitle:@"注册" forState:UIControlStateNormal];
        _btnRegister.frame = CGRectMake(regBtn_x, btn_y, btn_w, Cell_H);
        _btnRegister.layer.cornerRadius = CornerRadius;
        _btnRegister.clipsToBounds = YES;
        [_btnRegister setBackgroundColor:RGB_BLUE];
        [_btnRegister setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_btnRegister addTarget:self action:@selector(btnRegisterClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnRegister;
}

- (void)btnLoginClick
{
    if (self.loginBlock) {
        self.loginBlock();
    }
}

- (void)btnRegisterClick
{
    if (self.registerBlock) {
        self.registerBlock();
    }
}

+ (BOOL)isShowFirstGuideView
{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    BOOL isShowGuide = [user objectForKey:VERSION_IS_FIRSTGUIDE];
    return !isShowGuide;
}

+ (void)saveFirstGuideView
{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    [user setObject:@(YES) forKey:VERSION_IS_FIRSTGUIDE];
    [user synchronize];
}

+ (BOOL)isShowed
{
    // 读取版本信息
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *localVersion = [user objectForKey:VERSION_INFO_CURRENT];
    NSString *currentVersion = [[NSBundle mainBundle].infoDictionary objectForKey:@"CFBundleShortVersionString"];
    NSLog(@"localVersion = %@", localVersion);
    NSLog(@"currentVersion = %@", currentVersion);
//    if (localVersion == nil || ![currentVersion isEqualToString:localVersion]) {
    if (localVersion == nil) {
        [LaunchPagesVC saveCurrentVersion];
        return YES;
    }else
    {
        return NO;
    }
}

// 保存版本信息
+ (void)saveCurrentVersion
{
    NSString *version =[[NSBundle mainBundle].infoDictionary objectForKey:@"CFBundleShortVersionString"];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    [user setObject:version forKey:VERSION_INFO_CURRENT];
    [user synchronize];
}

#pragma mark - ScrollerView Delegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.pageControl.currentPage = scrollView.contentOffset.x / UI_SCREEN_WIDTH;
}

@end
